﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameServer.Managers;
using GameServer.Networking.Packets;
namespace GameServer.Networking.Handlers
{
    class HANDLE_CREDITS : PacketHandler
    {
        public override void Handle(Virtual_Objects.User.virtualUser User)
        {
            try
            {
                int OPCode = int.Parse(getNextBlock());
                if (OPCode == 1113) // When opens itemshop
                {
                    User.reloadCash();
                    User.send(new Packets.PACKET_CREDITS(User));
                }
                else if (OPCode == 1111) // Change Nick
                {
                    try
                    {
                        string NewNickname = getBlock(4);
                        string[] checkUsedNick = DB.runReadRow("SELECT * FROM users WHERE nickname='" + NewNickname + "'");
                        if (checkUsedNick.Length > 0)
                        {
                            User.send(new PACKET_CHANGE_NICKNAME());
                        }
                        else
                        {
                            User.Nickname = NewNickname;
                            Log.WriteLine("---" + User.Nickname + " it's now known as: " + NewNickname + "---");
                            User.send(new PACKET_CHANGE_NICKNAME(User, User.Nickname));
                            DB.runQuery("DELETE FROM inventory WHERE ownerid = '" + User.UserID + "' AND itemcode = 'CB01'");
                            DB.runQuery("UPDATE users SET nickname='" + NewNickname + "' WHERE id='" + User.UserID + "'");
                            User.Inventory = new InventoryItem[35];
                            User.send(new PACKET_DELETE_WEAPON(User, "CB01"));
                            User.LoadItems();
                            User.send(new PACKET_CREDITS(User));
                        }
                    }
                    catch (Exception Errore)
                    {
                        Console.WriteLine("MySQL Error: " + Errore);
                    }
                }
                else if (OPCode == 1110) // When buys something with cash
                {
                    if (Program.ESL)
                    {
                        User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, "NULL"));
                        return;
                    }
                    int Time = int.Parse(getNextBlock());
                    int Period = int.Parse(getNextBlock());
                    int CashToPay = int.Parse(getNextBlock());
                    string ItemCode = getNextBlock();
                    int[] convertDays = new int[4] { 3, 7, 15, 30 };

                    virtualItem Item = ItemManager.getItem(ItemCode);
                    if (Item != null)
                    {
                        if (User.Cash < Item.Cash)
                        {
                            User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.NotEnoughDinar, "NULL"));
                        }
                        /* else if (LevelCalculator.getLevelforExp(User.Exp) < Item.Level && User.Rank < 2)
                         {
                             User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.LevelLow, "NULL"));
                         }*/
                        else if (User.Cash > Item.Cash && Item.isValidShopItem == true && Item.Cash >= 0)
                        {
                            int InventorySlot = -1;
                            for (int I = 0; I < 30; I++)
                            {
                                if (User.Inventory[I] == null) { InventorySlot = I; break; }
                            }
                            int CostumeSlot = -1;
                            for (int I = 0; I < 30; I++)
                            {
                                if (User.Costume[I] == null) { CostumeSlot = I; break; }
                            }
                            if (InventorySlot >= 0)
                            {
                                #region SendItem
                                switch (ItemCode)
                                {
                                    case "CB03":
                                        {
                                            DB.runQuery("UPDATE users SET kills='0', deaths='0' WHERE id='" + User.UserID + "'");
                                            User.Kills = 0;
                                            User.Deaths = 0;
                                            User.send(new PACKET_CHARACTER_INFO(User));
                                            break;
                                        }
                                    case "CC10":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DQ02";
                                            string _Item2 = "BS0E";
                                            string _Item3 = "DF06";
                                            string _Item4 = "DF11";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddCostume(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC26":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DN03";
                                            string _Item2 = "DJ08";
                                            string _Item3 = "DJ05";
                                            string _Item4 = "DF13";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC12":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DH05";
                                            string _Item2 = "DC65";
                                            string _Item3 = "DB18";
                                            string _Item4 = "DS10";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC49":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DF14";
                                            string _Item2 = "DC40";
                                            string _Item3 = "DV01";
                                            string _Item4 = "DG37";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CR30":
                                        {
                                            if (InventorySlot < 5)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DF95";
                                            string _Item2 = "DE07";
                                            string _Item3 = "DG63";
                                            string _Item4 = "DB63";
                                            string _Item5 = "DJ13";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            User.AddItem(_Item5, convertDays[Period]);
                                            break;
                                        }
                                    case "CC22":
                                        {
                                            if (InventorySlot < 3)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DS10";
                                            string _Item2 = "DV01";
                                            string _Item3 = "DS01";
                                            string _Item4 = "BS0E";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC14":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DF18";
                                            string _Item2 = "DF08";
                                            string _Item3 = "DF20";
                                            string _Item4 = "DU01";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC16":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DF12";
                                            string _Item2 = "DC13";
                                            string _Item3 = "DT05";
                                            string _Item4 = "DG44";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC17":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DF37";
                                            string _Item2 = "DC64";
                                            string _Item3 = "DJ33";
                                            string _Item4 = "DG13";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    case "CC40":
                                        {
                                            if (InventorySlot < 4)
                                            {
                                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                                                return;
                                            }
                                            string _Item1 = "DF71";
                                            string _Item2 = "DC76";
                                            string _Item3 = "DT73";
                                            string _Item4 = "DG31";
                                            User.AddItem(_Item1, convertDays[Period]);
                                            User.AddItem(_Item2, convertDays[Period]);
                                            User.AddItem(_Item3, convertDays[Period]);
                                            User.AddItem(_Item4, convertDays[Period]);
                                            break;
                                        }
                                    default:
                                        {
                                            User.AddItem(ItemCode, convertDays[Period]);
                                            break;
                                        }
                                }
                                #endregion
                                User.Cash -= Item.Cash;
                                DB.runQuery("UPDATE users SET cash='" + User.Cash + "' WHERE id='" + User.UserID + "'");
                                User.reloadInventory();
                                #region ReloadItem
                                int ItemCount = 0;
                                User.Inventory = new InventoryItem[35];
                                User.reloadInventory();
                                User.LoadItems();
                                User.reloadInventory();
                                StringBuilder WeaponList = new StringBuilder();
                                for (int I = 0; I < 30; I++)
                                {
                                    if (User.Inventory[I] != null)
                                    {
                                        InventoryItem _Inv = User.Inventory[I];
                                        TimeSpan ExpireTime = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(_Inv._StartTime);

                                        WeaponList.Append(_Inv._Code.ToUpper() + "-3-0-" + _Inv._StartTime + "-0-0-0-0-0-9999-9999,");/*+ ExpireTime.TotalSeconds +*/
                                        ItemCount++;
                                    }
                                }

                                for (int J = 0; J < (30 - ItemCount); J++)
                                {
                                    WeaponList.Append("^,");
                                }

                                #endregion

                                User.send(new PACKET_ITEMSHOP(User, WeaponList.ToString().Remove(WeaponList.ToString().Length - 1)));
                                User.send(new Packets.PACKET_CREDITS(User));
                                Log.WriteLine(User.Nickname + " has bought [" + Item.WeaponCode.ToUpper() + "-" + Item.Name + "] for " + convertDays[Period] + "days.");
                            }
                            else
                            {
                                User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                            }
                        }
                        else
                        {
                            User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, "NULL"));
                        }
                    }
                    else
                    {
                        Log.WriteError(User.Nickname + " tried to buy: " + ItemCode + " but it's not available yet.");
                        User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, "NULL"));
                    }
                }
                else
                {
                    Log.WriteLine("New Cash ErrCode: " + OPCode);
                    Log.WriteLine(string.Join(" ", getAllBlocks()));
                }
            }
            catch (Exception ex)
            {
                Log.WriteError("MySQL Error: " + ex.Message);
            }
        }
    }
}
